package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.ProductionOrder;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.mapper.*;
import com.qidianit.mes.pro.service.ProductionOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/20 -15:47
 */
@Service
public class ProductionOrderServiceImpl implements ProductionOrderService {
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private UnitMapper unitMapper;


    @Override
    public Result<PageInfo<ProductionOrder>> queryList(ProductionOrder productionOrder, int pageNum, int pageSize) {
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //数据查询
        List<ProductionOrder> productionOrders = productionOrderMapper.queryList(productionOrder);

        for (ProductionOrder order : productionOrders) {
            order.setSourceDictData(dictDataMapper.queryByDictDataId(order.getProductionOrderSource()));
            order.setStatusDictData(dictDataMapper.queryByDictDataId(order.getProductionOrderStatus()));
            order.setTypeDictData(dictDataMapper.queryByDictDataId(order.getProductionOrderType()));
            if (customerMapper.selectByCode(order.getCustomerCode())!=null){
                order.setCustomer(customerMapper.selectByCode(order.getCustomerCode()));
            }
            if (materialMapper.queryByCode(order.getMaterialCode()) !=null){
                order.setMaterial(materialMapper.queryByCode(order.getMaterialCode()));
            }
            if(supplierMapper.queryByCode(order.getSupplierCode()) !=null){
                order.setSupplier(supplierMapper.queryByCode(order.getSupplierCode()));
            }
        }
        return Result.success(new PageInfo<>(productionOrders));
    }

    @Override
    public Result<ProductionOrder> queryById(Long id) {
        ProductionOrder productionOrder = productionOrderMapper.selectById(id);
        productionOrder.setSourceDictData(dictDataMapper.queryByDictDataId(productionOrder.getProductionOrderSource()));
        productionOrder.setStatusDictData(dictDataMapper.queryByDictDataId(productionOrder.getProductionOrderStatus()));
        productionOrder.setTypeDictData(dictDataMapper.queryByDictDataId(productionOrder.getProductionOrderType()));
        if (customerMapper.selectByCode(productionOrder.getCustomerCode())!=null){
            productionOrder.setCustomer(customerMapper.selectByCode(productionOrder.getCustomerCode()));
        }
        if (materialMapper.queryByCode(productionOrder.getMaterialCode()) !=null){
            productionOrder.setMaterial(materialMapper.queryByCode(productionOrder.getMaterialCode()));
            productionOrder.getMaterial().setUnit(unitMapper.queryById(productionOrder.getMaterial().getUnitId()));
        }
        if(supplierMapper.queryByCode(productionOrder.getSupplierCode()) != null){
            productionOrder.setSupplier(supplierMapper.queryByCode(productionOrder.getSupplierCode()));
        }
        return Result.success(productionOrder);
    }

    @Override
    public Result updateById(ProductionOrder productionOrder) {
        //编码不能重复
        ProductionOrder productionOrder1 = productionOrderMapper.selectByCode(productionOrder.getProductionOrderCode());

        if(productionOrder1!=null&&!productionOrder1.getId().equals(productionOrder.getId()) ){
            return Result.error(500,"编码已存在",null);
        }
        System.out.println("更新操作！！！");
        return productionOrderMapper.updateById(productionOrder)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result<ProductionOrder> addProductionOrder(ProductionOrder productionOrder) {
        //编码不能重复
        ProductionOrder productionOrder1 = productionOrderMapper.selectByCode(productionOrder.getProductionOrderCode());

        if(productionOrder1!=null ){
            return Result.error(500,"编码已存在",null);
        }
        return productionOrderMapper.insert(productionOrder)>0?Result.success(productionOrder):Result.error("操作失败");
    }

    @Override
    public Result delete(Long id) {
        return productionOrderMapper.deleteById(id)>0?Result.success():Result.error("操作失败");
    }


}
